package org.ekstrabilet.stadium.logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.ekstrabilet.database.connection.ConnectionManager;
import org.ekstrabilet.database.statements.Statements;
import org.ekstrabilet.stadium.beans.Seat;
/**
 * Singleton, represents logic of a single seat, provides 
 * connection between Seat object and database, implements 
 * methods responsible for manipulating that object
 * @author Marcin
 *
 */
public class SeatLogic {
	private static SeatLogic seatLogic= null;

	
	private SeatLogic(){
		
	}
	
	/**
	 * 
	 * @param x position x of seat in sector
	 * @param y position x of seat in sector
	 * @param sectorId
	 * @return Seat object
	 * @throws SQLException
	 */
	public Seat getSeat(int x, int y, int sectorId) throws SQLException{
		PreparedStatement stm;
		Seat seat= null;
		Connection conn= ConnectionManager.getManager().getConnection();
		try {
			stm = conn.prepareStatement(Statements.SELECT_SOLD_TICKET);
			stm.setInt(1, x);
			stm.setInt(2, y);
			stm.setInt(3, sectorId);
			ResultSet rs= stm.executeQuery();
			seat= new Seat();
			while(rs.next()){
				seat.setEmpty(false);
				return seat;
			}
			seat.setEmpty(true);
			
		} catch (SQLException e) {
			throw e;
		}
		return seat;

	}
	/**
	 * 
	 * @return instance of SeatLogic
	 */
	public static SeatLogic getInstance(){
		if(seatLogic == null)
			seatLogic= new SeatLogic();
		return seatLogic;
	}
}
